package com.hit.dissim.algorithm;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

public class findGenes {
	// static final File file = new File("C:\\myspace\\data\\table_goa.txt");/
	// static final File file = new File("FileResource/table_goa.txt");
	static String partpath = "\\FileResource\\table_goa.txt";
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		findGenes.find("GO:0016321", "GO:0030970");
	}

	/**
	 * 一个静态方法
	 * 
	 * @param id1
	 * @param id2
	 * @return
	 */
	public static List<String>[] find(String id1, String id2) {
		final File file = new File(getRootPath() + partpath);
//		System.out.println(file.getAbsolutePath());
		List<String> list1 = new ArrayList<String>();
		List<String> list2 = new ArrayList<String>();
		@SuppressWarnings("rawtypes")
		List[] res = { list1, list2 };
		try {
			Scanner in = new Scanner(new FileInputStream(file));
			in.nextLine();
			while (in.hasNext()) {
				String termid = in.next(); //
				String geneid = in.next(); //
				if (termid.equals(id1)) {
					if (!list1.contains(geneid))
						list1.add(geneid);
				}
				if (termid.equals(id2)) {
					if (!list2.contains(geneid))
						list2.add(geneid);
				}
			}
			in.close();
			System.out.println(list1.toString());
			System.out.println(list2.toString());

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//		System.out.println(res[0].toString());
//		System.out.println(res[1].toString());

		return res;
	}

	/**
	 * 获取webconent下的内容
	 * @return
	 */
	public static String getRootPath() {

		// 因为类名为"SaveAction"，因此" SaveAction.class"一定能找到

		String result = findGenes.class.getResource("findGenes.class").toString();

		int index = result.indexOf("WEB-INF");

		if (index == -1) {

			index = result.indexOf("bin");

		}

		result = result.substring(0, index);

		if (result.startsWith("jar")) {

			// 当class文件在jar文件中时，返回"jar:file:/F:/ ..."样的路径

			result = result.substring(10);

		} else if (result.startsWith("file")) {

			// 当class文件在class文件中时，返回"file:/F:/ ..."样的路径

			result = result.substring(6);

		}

		if (result.endsWith("/"))
			result = result.substring(0, result.length() - 1);// 不包含最后的"/"

		return result;

	}
}
